package miage.rest.resource.candidature;

import java.util.List;
import miage.rest.model.Statut;
import miage.rest.db.Database;
import miage.rest.xml.XstreamRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import com.thoughtworks.xstream.XStream;
import java.sql.SQLException;
import org.restlet.data.Status;

/**
 *
 * @author Lou
 */
public class StatutsResource extends ServerResource {

	private final Database database;
	private List<Statut> statuts;

	public StatutsResource(Database database) {
		super();
		this.database = database;
	}

	/** Initialisation de la ressource a partir de la chaine d'url */
	@Override
	protected void doInit() throws ResourceException {
		final String sql = "SELECT id_statut, nomstatut from statut";
		try {
			statuts = database.fetchAll(sql, Statut.class);
			if (statuts == null) {
				setExisting(false);
			}
		} catch (SQLException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
		}
	}

	/**
	 * Représentation XML en réponse à une requête GET.
	 * Jeu de caractères UTF-8
	 */
	@Get
	public Representation doGet() throws Exception {
		Representation response = null;
		//Création d'une représentation XStream de la liste de status
		XStream xstream = null;
		response = new XstreamRepresentation<List<Statut>>(statuts);
		xstream = ((XstreamRepresentation) response).getXstream();
		xstream.alias("statuts", List.class, statuts.getClass());
		xstream.processAnnotations(Statut.class);
		
		return response;
	}
}
